import { ObjectEditor } from '@/components/index.jsx';
import { Button, Flex } from 'antd';
import { SPACE } from '@/constants/themeConstant.js';
import ModalUtil from '@/utils/modal.js';
import StringUtil from '@/utils/string.js';

/**
 * 存储配置编辑器
 *
 * @param {*} props 属性
 * @param {string} props.serviceProvider 服务商
 */
export default function StorageConfigEdit(props) {
  const { id, onChange, value, serviceProvider } = props;

  function handleImportConfig() {
    let config = {};
    switch (serviceProvider) {
      case 'local':
        config = { storage_path: '' };
        break;
      case 'minio':
        config = { endpoint: '', access_key: '', secret_key: '', bucket_name: '', region: '' };
        break;
    }
    onChange && onChange(config);
  }

  return (
    <Flex vertical gap={SPACE}>
      <ObjectEditor
        id={id}
        initialObject={value ?? {}}
        onObjectChange={(obj) => {
          onChange && onChange(obj);
        }}
        keyTitle={'属性'}
        valueTitle={'值'}
      />
      <Button
        disabled={StringUtil.isEmpty(serviceProvider)}
        color="default"
        onClick={() => {
          ModalUtil.confirm('此操作将会覆盖当前配置，确定要导入预设参数吗？', () => {
            handleImportConfig();
          });
        }}
      >
        导入预设参数
      </Button>
    </Flex>
  );
}
